package cn.haona.game.simb.wap.servlet.init.object;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.haona.game.simb.object.god.CacheManager;
import cn.haona.game.simb.object.god.MainManager;
import cn.haona.game.simb.object.god.PersistenceManager;
import cn.haona.game.simb.object.god.cache.CacheImpl_Memcached;
import cn.haona.game.simb.servlet.Util;

public class InitDB {

	final public static String TAG_CONNECT_URL="connectURI";
	final public static String TAG_USERNAME="username";
	final public static String TAG_PASSWORD="password";
	final public static String TAG_DRIVERCLASS="driverClass";
	final public static String TAG_INIT_SIZE="initialSize";
	
	final public static String TAG_MAXACTIVE="maxActive";
	final public static String TAG_MAXIDLE="maxIdle";
	final public static String TAG_MAXWAIT="maxWait";
	
	
	public void initMe(String fileName)
	{
		// 字符串
		String strXML = Util.getStringFromLoadFile(fileName);

		// 解析数据
		Document document;
		try {
			document = DocumentHelper.parseText(strXML);
			// 跟元素
			Element root = document.getRootElement();
			
			String connectURI=root.elementTextTrim(TAG_CONNECT_URL);
			String username=root.elementTextTrim(TAG_USERNAME);
			String password=root.elementTextTrim(TAG_PASSWORD);
			String driverClass=root.elementTextTrim(TAG_DRIVERCLASS);
			String initialSize=root.elementTextTrim(TAG_INIT_SIZE);
			
			String maxActive=root.elementTextTrim(TAG_MAXACTIVE);
			String maxIdle=root.elementTextTrim(TAG_MAXIDLE);
			String maxWait=root.elementTextTrim(TAG_MAXWAIT);
			
			PersistenceManager pManager=new PersistenceManager();
			pManager.setConnectURI(connectURI);
			pManager.setDriverClass(driverClass);
			pManager.setInitialSize(Integer.parseInt(initialSize));
			pManager.setMaxActive(Integer.parseInt(maxActive));
			pManager.setMaxIdle(Integer.parseInt(maxIdle));
			
			pManager.setMaxWait(Long.parseLong(maxWait));
			pManager.setPassword(password);
			pManager.setUsername(username);
			pManager.initDB();
			
			//放入持久化管理器
			MainManager.getInstance().setPersistenceManager(pManager);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
